<?php
namespace app\api\model;

use Exception;

class Code extends Base
{
    protected $name = 'code';
    protected $autoWriteTimestamp = false;
    
    /**
     * 验证码验证
     * @param string $phone 手机号
     * @param int    $code  验证码
     * @return array
     */
    public function checkCode($phone, $code)
    {
        try{
            $res = $this->getFind(['phone' => $phone, 'is_use' => 0], '*', 'id desc');

            if ($res['code'] != 'ok') {
                return ['code' => 'fail','msg' => "系统错误，errMsg:".$res['msg']];
            }

            if ($code != $res['data']['code']) {
                return ['code' => 'fail','msg' => '验证码错误'];
            }

            if (time() > $res['data']['expiration_time']) {
                return ['code' => 'fail','msg' => '验证码已过期'];
            }

            return ['code' => 'ok'];
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }
}